package java;

import java.util.HashMap;
import java.util.Map;

public class Question3 {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("ohomm"));
    }
    private static int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        Map<Character,Integer> record = new HashMap<>();
        Integer maxLength = 0;
        Integer count = 0;
        Integer start = 0;

        for (int i = 0; i < chars.length;i++){
            if (!record.containsKey(chars[i])){
                if (start == 0){
                    count ++;
                }
                record.put(chars[i],i);
            }else {
                if (record.get(chars[i]) >= start){
                    if (maxLength < i - start){
                        maxLength = i - start;
                    }
                    start = record.get(chars[i]) + 1;
                }
                record.put(chars[i],i);
            }
        }
        if (maxLength < count){
            maxLength = count;
        }
        if (maxLength < chars.length - start){
            maxLength = chars.length - start;
        }
        return maxLength;
    }
}
